package com.jack.payment.aspect.common;

import com.alibaba.fastjson.JSONObject;
import com.jack.payment.entity.business.UriDescribeBo;
import com.jack.payment.entity.common.BusinessException;
import com.jack.payment.entity.common.CommonResult;
import com.jack.payment.entity.mong.RequestLogModel;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
public class AspectHandlerUtil {
    private Logger log = LoggerFactory.getLogger(AspectHandlerUtil.class);


    /** API 开放*/
    private static final String API_MODULE_PATH = "com.jack.payment.controller.client";
    /** 回调*/
    private static final String WEBHOOK_MODULE_PATH = "com.jack.payment.controller.notify";
    /** 自定义异常*/
    private static final String EXCEPTION_CLASS_NAME = "com.jack.payment.entity.common.BusinessException";


    /**
     *  自定义异常拦截
     * @param uri
     * @param e
     * @return
     */
    public CommonResult exceptionClassJudgeVerify(String uri,
                                                  Exception e) {
        String exceptionName = e.getClass().getName();
        if (EXCEPTION_CLASS_NAME.equals(exceptionName)) {
            BusinessException businessException = (BusinessException) e;
            return CommonResult.error(
                    businessException.getCode(),
                    businessException.getMessage()
            );
        } else {
            return CommonResult.error(-300, "Server Error");
        }
    }


    /**
     * 接口信息
     * @param method
     * @param api
     * @return
     */
    public UriDescribeBo getUriDetail(Method method, String api) {
        Operation apiOperation = method.getAnnotation(Operation.class);
        return new UriDescribeBo().builder()
                .apiDescribe(apiOperation.summary())
                .uri(api)
                .build();
    }

    public RequestLogModel builderRequestLogModel(Integer requestType,
                                                  Long startTime,
                                                  UriDescribeBo uriDescribe,
                                                  HttpServletRequest request,
                                                  Object[] args,
                                                  String requestIp,
                                                  Integer status,
                                                  String message) {
        Long timeConsuming = System.currentTimeMillis() - startTime;
        RequestLogModel requestLogModel = new RequestLogModel().builder()
                .createTime(startTime / 1000)                               // 请求时间
                .timeConsuming(timeConsuming)                               // 请求耗时
                .requestType(requestType)                                   // [0 : login, 1: ADMIN, 2: WEBHOOK, 3.API]
                .requestMethod(request.getMethod())                         // 请求方式 get post
                .requestParam(JSONObject.toJSONString(args))                // 本次请求参数
                .requestIp(requestIp)               // 请求的ip地址
                .uriExplain(uriDescribe.getApiDescribe())                   // 请求的 uri 说明
                .uriPath(uriDescribe.getUri())                              // 请求的 uri 地址
                .status(status)                                             // 请求状态
                .message(message)                                           // 错误消息
                .build();
        return requestLogModel;
    }
}
